package com.tonglin.springboot.service;

import com.tonglin.springboot.controller.BookAdressController;
import com.tonglin.springboot.mapper.BookMapper;
import com.tonglin.springboot.mapper.BookOrderMapper;
import com.tonglin.springboot.mapper.MsgMapper;
import com.tonglin.springboot.po.Book;
import com.tonglin.springboot.po.BookOrder;
import com.tonglin.springboot.po.Msg;
import com.tonglin.springboot.po.Status;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 1.从数据库中获取所有书籍的名字
 * 2.将书籍名字传到controller层，然后将controller层把数据展示到页面
 *
 *
 *
 *
 */
@Service
public class BookService {


    @Autowired
    BookMapper bookMapper;

    @Autowired
    BookOrderMapper bookOrderMapper;

    @Autowired
    MsgMapper msgMapper;

    BookAdressController bookAdressController = new BookAdressController();

    //1.从数据库中获取所有书籍的名字
    public List<Book> showBooks() {


        List<Book> list = bookMapper.select();
        for (Book  book   : list) {

            System.out.println(book.getBookId());

        }

        return list;
    }
    //2.从前台接受订单信息，根据订单信息，修改book表中的书数量
    //生成一张新的订单数据存在数据库中

    /**
     * 第一步判断用户名是否为空，为空的话向页面显示“用户名为空，请重新填写用户信息”
     * 第二步判断数据库中书的存量还够不够
     * 存量不够则返回该书已经没有了
     * 如果存量够的话，则返回借阅成功页面
     * 第三步将订单信息插入到book_order数据中
     *
     * @param userName
     * @param bookName
     */
    public String updateOrder(String userName, String bookName) {

        //用户名判空
        String msg;
        if (StringUtils.isBlank(userName)) {

            msg = "用户名不能为空！请重新填写用户信息！";

            return msg;
        }
        //查看书籍存量是否还够

        Book book = bookMapper.queryBookByname(bookName);
        if (book==null) {

            msg = "该书不存在！";

            return msg;
        }


        if(book.getBookNumber()<1){

            msg = "该书存量不足！";
            return msg;
        }

        //如果数据库订单表中已经有了用户名和书名则不允许借书

        List<BookOrder> bookOrder1 = bookOrderMapper.queryBookOrderByname(bookName);


        //订单中有用户名和书名则不插入


        for (BookOrder bookOrd: bookOrder1
             ) {

            System.out.println("借书测试！");
            System.out.println(bookOrd.getUserName());
            System.out.println(bookName);
            if(bookOrder1!=null&&(bookOrd.getUserName().equals(userName))){

                msg = "你已经借过此书！";

                return msg;

            }

        }




        //将订单信息插入到订单表

        BookOrder bookOrder = new BookOrder();
        bookOrder.setBookName(bookName);
        bookOrder.setUserName(userName);

        int temp = bookOrderMapper.insert(bookOrder);

        if (temp < 1) {

            msg = "订单生成失败，请检查借阅信息！";

            return msg;

        }




        msg = "借阅成功！";

        //数据库中书的数量减1

        book.setBookNumber(book.getBookNumber()-1);
        bookMapper.updateNumberJ(book);

        return msg;

    }

    /**
     * 将用户搜索的书籍信息返回到页面
     *
     *     1.判断用户搜索的书籍是否存在数据库中
     *        1.不存在则提示该书籍不存在
     *        2.存在的话则返回书籍
     *
     *
     */

    public Status returnSearchBook(String bookName) {

        Book book = bookMapper.queryBookByname(bookName);

        Status status = new Status();

        if(StringUtils.isBlank(bookName)){

            status.setMsg("搜索内容不能为空");
            status.setCode(false);
            return status;
        }

        if(book==null){
            status.setMsg("该书籍不存在！");
            status.setCode(false);
            return status;

        }

        status.setCode(true);
        status.setObject(book);

        return status;
    }

    /**
     * 还书
     * 1.页面会返回一个用户名和一个书名
     *   此时应该执行一下的操作
     *     1.通过用户姓名查询，将该条订单删除
     *     2.通过书名查询，将书的借阅状态改为未借阅
     *
     *
     */

    public String doReturn(String userName , String bookName){


        BookOrder bookOrder = new BookOrder();
        bookOrder.setUserName(userName);
        bookOrder.setBookName(bookName);

        //用户名判空
        String msg;
        if (StringUtils.isBlank(userName)) {

            msg = "用户名不能为空！请重新填写用户信息！";

            return msg;
        }
          //判断还的书是不是之前借的书

        List<BookOrder> bookOrder1 = bookOrderMapper.queryBookOrderByname(bookName);
        if(bookOrder1==null){
            msg = "该书未被借过！";
            return msg;
        }

        for (BookOrder boo: bookOrder1
             ) {

            System.out.println("书名测试开始！");
            System.out.println(boo.getUserName());

            if(boo.getUserName().equals(userName)){

                //通过用户名查询，把订单删除

                bookOrderMapper.delete(bookOrder);

                //数据库中书籍的数量加1
                Book book = bookMapper.queryBookByname(bookName);
                book.setBookNumber(book.getBookNumber()+1);
                bookMapper.updateNumber(book);
                msg="还书成功！";

                return msg;

            }

        }

        msg = "借的书与还的书有冲突！";

        return msg;
    }

    /**
     *
     * 把接受用户留言的消息
     *
     * 对留言进行判空
     *
     * 然后将留言信息插入数据库
     *
     *
     */

    public String leaveMsg(String lMsg,String userName){
        String msg;

        //对留言判空
        if(StringUtils.isBlank(lMsg)){

            msg="不能提交空留言！";
            return msg;
        }

        Msg msg1 = new Msg();
        msg1.setMsg(lMsg);
        msg1.setUserName(userName);
        msgMapper.insert(msg1);

        msg = "提交成功！";

        return  msg;

    }




}